package com.yam.michael.service.impl;

import java.util.Iterator;
import java.util.List;

import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.xunjia.exception.XunjiaException;
import com.xunjia.util.Constants;
import com.yam.michael.hibernate.dao.*;
import com.yam.michael.hibernate.pojo.*;
import com.yam.michael.service.IUserService;
public class UserServiceImpl implements IUserService {
	private UserInfoDao userInfoDao;
	private WalletDao walletDao;
	private CompanyDao companyDao;
	private RightsDao rightsDao;
	private KeywordsDao keywordsDao;
	private AdvActivityDao advActivityDao;
	private AdvSerialDao advSerialDao;
	private HyperlinkAdvInfoDao hyperlinkAdvInfoDao;
	public void setAdvSerialDao(AdvSerialDao advSerialDao) {
		this.advSerialDao = advSerialDao;
	}
	public void setHyperlinkAdvInfoDao(HyperlinkAdvInfoDao hyperlinkAdvInfoDao) {
		this.hyperlinkAdvInfoDao = hyperlinkAdvInfoDao;
	}

	public void setAdvActivityDao(AdvActivityDao advActivityDao) {
		this.advActivityDao = advActivityDao;
	}

	public void setKeywordsDao(KeywordsDao keywordsDao) {
		this.keywordsDao = keywordsDao;
	}

	public void setRightsDao(RightsDao rightsDao) {
		this.rightsDao = rightsDao;
	}

	public void setUserInfoDao(UserInfoDao userInfoDao) {
		this.userInfoDao = userInfoDao;
	}

	public void setWalletDao(WalletDao walletDao) {
		this.walletDao = walletDao;
	}

	public void setCompanyDao(CompanyDao companyDao) {
		this.companyDao = companyDao;
	}
	/**
	 * 用户登陆
	 */
	@Transactional(isolation = Isolation.SERIALIZABLE, propagation = Propagation.REQUIRED, rollbackFor = { XunjiaException.class })
	@Override
	public UserInfo login(String loginName) throws XunjiaException {
		UserInfo userInfo = userInfoDao.getUserInfoById(loginName);
		//如果为企业用户
		if(loginName.startsWith("TC"))
		{
			//查询并返回公司信息
			Company company = companyDao.getCompanyById(userInfo.getCompanyID());
			//将公司信息保存到userInfo中
			userInfo.setCompany(company);
			//查询并返回权限信息
			Rights rights = rightsDao.getRightsById(loginName);
			//将权限信息保存到userInfo中
			userInfo.setRights(rights);
		}
		return userInfo;
	}
	@Transactional(isolation = Isolation.SERIALIZABLE, propagation = Propagation.REQUIRED, rollbackFor = { XunjiaException.class })
	@Override
	public UserInfo register(String type, UserInfo userInfo, Company company,Wallet wallet,HyperlinkAdvInfo hyperlinkAdvInfo/*付款类型,信用卡,应付金额*/)
			throws XunjiaException {
		userInfo.setWallet(wallet);
		userInfo.setPassword("123456789");
		//新建用户,返回用户信息
		UserInfo userInfo2 = userInfoDao.addUserInfo(userInfo, type);
		if("TC".equals(type))
		{
			//添加公司,返回公司ID
			Long companyID = companyDao.addCompany(company);
			//添加权限,返回权限ID
			Rights rights =  new Rights();
			rights.setAdvertisementable("1");
			rights.setReportable("1");
			rights.setWalletable("1");
			rights.setUserId(userInfo2.getLoginName());
			//保存权限
			rightsDao.addRights(rights);
			userInfo2.setCompanyID(companyID);
			userInfoDao.updateUserInfo(userInfo2);
		}
		//添加用户完成,添加用户购买的超链接广告
		buyAHyperlinkAdv(hyperlinkAdvInfo);
		//建立完成,注册成功,返回用户信息
		return userInfo2;
	}
	@Transactional(isolation = Isolation.SERIALIZABLE, propagation = Propagation.REQUIRED, rollbackFor = { XunjiaException.class })
	@Override
	/**
	 * 购买超链接广告
	 */
	public void buyAHyperlinkAdv(HyperlinkAdvInfo hyperlinkAdvInfo)
			throws XunjiaException {
		hyperlinkAdvInfoDao.addHyperlinkAdvInfo(hyperlinkAdvInfo);
	}
	@SuppressWarnings("unchecked")
	@Override
	/**
	 * 购买关键字广告
	 */
	public void buyAKeywordsAdv(List<KeywordsAdvInfo> keywordsAdvInfo,List<Keywords> keyword)
			throws XunjiaException {
			Iterator itk = keyword.iterator();
			while(itk.hasNext())
			{
				Iterator it = keywordsAdvInfo.iterator();
				Keywords keywords1 = (Keywords) itk.next();
				 while(it.hasNext())
				 {	
					 keywords1.getKeywordsAdvInfo().add(it.next());
				 }
				 keywordsDao.addKeywords(keywords1);
			}
	}
	/**
	 * 按关键字搜索关键字广告
	 * 供询价外网API使用
	 * @return
	 * @throws XunjiaException
	 */
	@Transactional(isolation = Isolation.SERIALIZABLE, propagation = Propagation.REQUIRED, rollbackFor = { XunjiaException.class })
	@Override
	public List<Keywords> getKeywordsAdvByKeywordsLike(
			String keywords, int size,String type) throws XunjiaException {
		return keywordsDao.searchKeywordsByLike(keywords, size, type);
	}
	@Transactional(isolation = Isolation.SERIALIZABLE, propagation = Propagation.REQUIRED, rollbackFor = { XunjiaException.class })
	@Override
	/**
	 * 
	 * @param type 注册类型,TC为企业用户 ,TB为个人用户
	 * @param userInfo  用户信息
	 * @param company  如果为企业用户注册,企业信息,个人用户为空
	 * @param wallet   钱包信息
	 * @param keywordslist  关键字列表
	 * @param keywordsAdvInfo  关键字信息
	 * @return
	 * @throws XunjiaException
	 */
	public UserInfo register(String type, UserInfo userInfo, Company company,
			Wallet wallet, List<Keywords> keywordslist,
			List<KeywordsAdvInfo> keywordsAdvInfoList) throws XunjiaException {
		userInfo.setWallet(wallet);
		userInfo.setPassword(Constants.DEFAULT_PASSWORDS);
		//新建用户,返回用户信息
		UserInfo userInfo2 = userInfoDao.addUserInfo(userInfo, type);
		Long sid = null;
		if("TC".equals(type))
		{
			//添加公司,返回公司ID
			Long companyID = companyDao.addCompany(company);
			//添加权限,返回权限ID
			Rights rights =  new Rights();
			//设置拥有三种权限
			rights.setAdvertisementable(Constants.RIGHTS_ABLE);
			rights.setReportable(Constants.RIGHTS_ABLE);
			rights.setWalletable(Constants.RIGHTS_ABLE);
			rights.setUserId(userInfo2.getLoginName());
			//保存权限
			rightsDao.addRights(rights);
			userInfo2.setCompanyID(companyID);
			userInfoDao.updateUserInfo(userInfo2);
			//添加用户完成,建立广告系列
			//企业用户,广告系列为companyID(企业ID)
			AdvSerial advSerial = new AdvSerial();
			advSerial.setMaxPayPerDay(wallet.getMaxPayPerDay());
			advSerial.setOwnerUserId(Long.toString(userInfo2.getCompanyID()));
			advSerial.setSeriesName(Constants.DEFAULT_SERIAL_NAME);
			advSerial.setStatus(Constants.STATUS_USEABLE);
			sid = advSerialDao.addAdvSerial(advSerial);	
		}
		//如果为个人用户,则广告系列为个人登录账号
		else if("TB".equals(type))
		{
			//添加用户完成,建立广告系列
			AdvSerial advSerial = new AdvSerial();
			advSerial.setMaxPayPerDay(wallet.getMaxPayPerDay());
			advSerial.setOwnerUserId(userInfo2.getLoginName());
			advSerial.setSeriesName(Constants.DEFAULT_SERIAL_NAME);
			advSerial.setStatus(Constants.STATUS_USEABLE);
			sid = advSerialDao.addAdvSerial(advSerial);
		}
		//添加系列完成,建立广告活动
		AdvActivity advActivity = new AdvActivity();
		advActivity.setActivityName(Constants.DEFAULT_ACTIVITY_NAME);
		advActivity.setActivityStatus(Constants.STATUS_USEABLE);
		advActivity.setCostPerMonth(0.00);
		advActivity.setMaxPayPerDay(wallet.getMaxPayPerDay());
		advActivity.setSid(sid);
		advActivityDao.addAdvActivity(advActivity);
		//添加广告完成,建立广告
		buyAKeywordsAdv(keywordsAdvInfoList, keywordslist);
		return userInfo2;
	}
	@Transactional(isolation = Isolation.SERIALIZABLE, propagation = Propagation.REQUIRED, rollbackFor = { XunjiaException.class })
	@Override
	/**
	 * 按广告位置获取超链接广告信息
	 * @param pisition广告位置ID
	 * @return
	 * @throws XunjiaException
	 */
	public List<HyperlinkAdvInfo> getHyperlinkAdvInfoByPosition(String position)
			throws XunjiaException {
		return hyperlinkAdvInfoDao.getHyperlinkAdvInfoByPositon(position);
	}
}
